#include <QCoreApplication>
#include <QTextCodec>
#include "module/cnetworkthread.h"
#include "clog.h"
#include "serversetting.h"

//#include "ttg_ips/cttgipsconnector.h"
//#include "ttg_ips/cpackagecrc.h"

//#include "locol_ips/clocolips.h"
#include "aes/aestools.h"
#include "cudpcmd.h"

int main(int argc, char *argv[])
{

    //qInstallMessageHandler(&CLog::debugOutput);

    QCoreApplication a(argc, argv);

    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

    CLog::getSingle();

    QString word = "灯光亮度30%";
    QRegExp rx("[0-9]{1,2}%");
    QString value;
    if(rx.indexIn(word) != -1) {
         value = rx.cap(0);
    }

//    QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
//    QTextCodec* gbk = QTextCodec::codecForName("gbk");

//    QString b64 = "5YiY5b635Y2OKOa1i+ivleWuouaOpyk=";//"5byg5a2m5Y+L";
//    QByteArray bin64 = QByteArray::fromBase64(b64.toLocal8Bit());

//    QString strUnicode= utf8->toUnicode(bin64.data());
//    //2. unicode -> gbk, 得到QByteArray
//    QByteArray gb_bytes= gbk->fromUnicode(strUnicode);

//    QString reply = QString::fromUtf8(strUnicode.toLocal8Bit().data());

//    qDebug() << strUnicode  << "  -  "  << QString(bin64) << QDateTime::currentDateTime().toString();
//    CLocolIPS tlocal;
//    _u8 cmdBytes[20] = {0xaa,0x55,0x07,0xff,0xff,0xfb,0x04,
//                     0xaa,0x55,0x0d,0xbc,0x1c,0x64,0x5f,0xfa,0xe1,0x00,0x80,0xff,0x0};

//    QList<QByteArray> cmdList = tlocal.splitDatagram((unsigned char *)cmdBytes,20);

//    for(int i=0;i<cmdList.size();i++)
//    {
//        QByteArray d = cmdList.at(i);
//        tlocal.ttg_data_analyze((_u8 *)d.data(),d.size());
//    }

    //qDebug() << "GateWay start: version 4.0 - new watch dog - auto restart 2d &  update";
    //qDebug() << "GateWay start: version " << 5.0 << "- TTG_st support";
    //qDebug() << "GateWay start: version " << 6.0 << "- freash status & local cmd support";
    qDebug() << "GateWay start: version " << g_version << " - fix freash status & release test";

//    if(ServerSetting::defaultSetting()->_serial.isEmpty())
//    {
//        qDebug() << "Serial is not correct!";
//    }
//    else
    {
        CUdpCmd local_udp;
        local_udp.ready(9999);

        CNetworkThread *netThread = new CNetworkThread();
        netThread->start();

        if(netThread->isRunning())
            return a.exec();
    }

    return 0;

//       QString key = "1234567812345678";
//       AESTools *aes = new AESTools(&key);
//       QString mw = "TTG-SZ-0001";
//       QString b64 = aes->Encrypt(&mw);

//       //打印信息
//       qDebug() << "mingwen:" << b64.toUpper();
//       qDebug() << "jiemi:" << aes->Decrypt(&b64);

//    char _addr_buf[2]={0};
//    cmei_set_group_address((unsigned char *)_addr_buf,"1/5/51");

//    char valuedata[1] = {0x01};

//    _u8 statusData[32] = {0xAA,0x55,0x0D,0xBC,0x11,0x14,0x0D,0x62,0xE1,0x00,0x81,0x49,0x00};

//    CLocolIPS ips;
//    ips.ttg_data_analyze(statusData,0x0D);
//    ips.doWriteDevice(QByteArray(_addr_buf,2),ONE_BIT_VALUE,QByteArray(valuedata,1));

//    CHttpManger *manage = new CHttpManger();
//    manage->httpPostSend("hi");

//    //网关长连接
//    CTcpConnect *tcpConn = new CTcpConnect();
//    //打洞
//    CHoleMakeBeat *holeMake = new CHoleMakeBeat();

//    if(holeMake->initState)
//    {
//        connect(holeMake,SIGNAL(s_reqTcpConnect()),tcpConn,SLOT(doConnectToServer()));
//        exec();
//    }
}
